const FoodModel = require('../model/food')

exports.add = async (req, res, next) => {
    try {
        let result = await FoodModel.add(req.fields)
        // console.log(result);
        res.send({ status: 'success', msg: '食品添加成功', data: { id: result[0].insertId } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '食品添加失败' })
    }
}

exports.edit = async (req, res, next) => {
    try {
        let result = await FoodModel.edit(req.params.id, req.fields)
        res.send({ status: 'success', msg: '食品修改成功', data: { affectedRows: result[0].affectedRows } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '食品修改失败' })
    }
}

exports.delete = async (req, res, next) => {
    try {
        // console.log(req.params);
        let result = await FoodModel.delete(req.params.id)
        res.send({ status: 'success', msg: '食品删除成功', data: { affectedRows: result[0].affectedRows } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '食品删除失败' })
    }
}

exports.check = async (req, res, next) => {
    try {
        let result = await FoodModel.check(req.params.food_name)
        if (result.length === 0) {
            res.send({ status: 'success', msg: '食品名可用', data: result })
        } else {
            next({ status: 'fail', msg: '食品名重复' })
        }
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '检查食品失败' })
    }
}

exports.find = async (req, res, next) => {
    try {
        // console.log(req.params);
        let result = await FoodModel.find(req.params.id)
        res.send({ status: 'success', msg: '食品查找成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '食品查找失败' })
    }
}

exports.getTotalCount = async (req, res, next) => {
    try {
        let result = await FoodModel.getTotalCount()
        res.send({ status: 'success', msg: '食品查找成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '食品查找失败' })
    }
}

exports.findByPage = async (req, res, next) => {
    try {
        // console.log(req.params);
        let result = await FoodModel.findByPage(req.query)
        let total = await FoodModel.getTotalCount()
        let totalPage = Math.ceil(total[0].total / 10)
        // console.log(totalPage);
        res.send({ status: 'success', msg: '食品查找成功', data: { list: result, totalPage } })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '食品查找失败' })
    }
}

exports.uploadFoodImg = async (req, res, next) => {
    if (req.files.food_img.size === 0) return next({ status: 'fail', msg: '没有提交任何图片' })
    try {
        let result = await FoodModel.uploadFoodImg(req.files)
        // console.log(result);
        res.send({ status: 'success', msg: '图片上传成功', data: result })
    } catch (error) {
        console.log(error);
        next({ status: 'fail', msg: '图片上传失败' })
    }
}